package chessBoard;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

/**
 * Panel pro podavani informaci o probihajici hre
 * 
 * @author dove
 * 
 */
public class InfoPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private String language;

	private ResourceBundle messages;

	private Preferences prefs;

	private String name1;
	private String name2;

	private String rating1 = "";
	private String rating2 = "";

	private int time1 = 0;
	private int time2 = 0;

	private String materialStrength1 = "39";
	private String materialStrength2 = "39";

	private boolean firstActive = false;
	private boolean secondActive = false;
	private boolean flip = true;

	private Dimension size = new Dimension(200, 600);
	private int position = 420;
	private int height = 400;

	private Color blue = new Color(110, 129, 158);

	// private static Logger logger = Logger.getLogger("Log");

	public InfoPanel() {
		this.setSize(size);

		prefs = Preferences.userRoot();
		language = prefs.get("Language", "en");
		Locale currentLocale = new Locale(language);
		messages = ResourceBundle.getBundle("Lang", currentLocale);
		name1 = messages.getString("player1");
		name2 = messages.getString("player2");
	}

	public InfoPanel(final Dimension size) {
		this.size = size;
		this.setSize(size);
	}

	public final void paint(final Graphics g) {

		g.clearRect(position, 0, size.width + position, size.height);

		Font big = new Font("serif", Font.BOLD, 20);
		Font small = new Font("serif", Font.PLAIN, 14);
		g.setColor(Color.BLACK);
		g.setFont(big);

		if (flip) {
			g.drawString(name1, position, height / 10);
			g.drawString(name2, position, (height / 10) * 9);
			g.setFont(small);
			g.drawString(rating1, position, (height / 10) - 25);
			g.drawString(rating2, position, ((height / 10) * 9) + 15);
			g.drawString(materialStrength1, position, ((height / 10) * 5) - 25);
			g.drawString(materialStrength2, position, ((height / 10) * 5) + 15);
			g.setColor(blue);
			g.fillRoundRect(position + 25, ((height / 10) * 5) - 38, Integer
					.valueOf(materialStrength1) * 3, 15, 5, 5);
			g.fillRoundRect(position + 25, ((height / 10) * 5) + 2, Integer
					.valueOf(materialStrength2) * 3, 15, 5, 5);
			g.setColor(Color.BLACK);
			g.drawRoundRect(position + 25, ((height / 10) * 5) - 38, Integer
					.valueOf(materialStrength1) * 3, 15, 5, 5);
			g.drawRoundRect(position + 25, ((height / 10) * 5) + 2, Integer
					.valueOf(materialStrength2) * 3, 15, 5, 5);

		} else {
			g.drawString(name1, position, (height / 10) * 9);
			g.drawString(name2, position, height / 10);
			g.setFont(small);
			g.drawString(rating1, position, ((height / 10) * 9) + 15);
			g.drawString(rating2, position, (height / 10) - 25);
			g.drawString(materialStrength2, position, ((height / 10) * 5) - 25);
			g.drawString(materialStrength1, position, ((height / 10) * 5) + 15);
			g.setColor(blue);

			g.fillRoundRect(position + 25, ((height / 10) * 5) - 38, Integer
					.valueOf(materialStrength2) * 3, 15, 5, 5);
			g.fillRoundRect(position + 25, ((height / 10) * 5) + 2, Integer
					.valueOf(materialStrength1) * 3, 15, 5, 5);
			g.setColor(Color.BLACK);
			g.drawRoundRect(position + 25, ((height / 10) * 5) + 2, Integer
					.valueOf(materialStrength1) * 3, 15, 5, 5);
			g.drawRoundRect(position + 25, ((height / 10) * 5) - 38, Integer
					.valueOf(materialStrength2) * 3, 15, 5, 5);

		}
		g.setFont(big);

		String time = "";
		time += String.valueOf(time1 / 60);
		time += " : ";
		if (time1 % 60 < 10) {
			time += "0" + String.valueOf(time1 % 60);
		} else {
			time += String.valueOf(time1 % 60);
		}
		if (flip) {
			g.drawString(time, position, (height / 10) * 2);
		} else {
			g.drawString(time, position, (height / 10) * 8);
		}

		time = "";
		time += String.valueOf(time2 / 60);
		time += " : ";
		if (time2 % 60 < 10) {
			time += "0" + String.valueOf(time2 % 60);
		} else {
			time += String.valueOf(time2 % 60);
		}
		if (flip) {
			g.drawString(time, position, (height / 10) * 8);
		} else {
			g.drawString(time, position, (height / 10) * 2);
		}
	}

	public final void setSize(final Dimension size) {
		this.size = size;
	}

	public final Dimension getSize() {
		return size;
	}

	/**
	 * Nastavuje jmeno prvniho hrace
	 * 
	 * @param name1
	 *            jmeno prvniho hrace
	 */
	public final void setName1(final String name1) {
		this.name1 = name1;
		repaint();
	}

	/**
	 * Nastavuje cas prvniho hrace
	 * 
	 * @param time1
	 *            cas v sekundach
	 */
	public final void setTime1(final int time1) {
		this.time1 = time1;
		repaint();
	}

	/**
	 * Nastavuje jmeno druheho hrace
	 * 
	 * @param name1
	 *            jmeno druheho hrace
	 */
	public final void setName2(final String name2) {
		this.name2 = name2;
		repaint();
	}

	/**
	 * Nastavuje cas druheho hrace
	 * 
	 * @param time2
	 *            cas v sekundach
	 */
	public final void setTime2(final int time2) {
		this.time2 = time2;
		repaint();
	}

	/**
	 * Ubere sekundu z prvniho casu
	 */
	public final void decreaseTimeOne() {
		if (time2 > 0) {
			time2--;
		}
		repaint();
	}

	/**
	 * Ubere sekundu z druheho casu
	 */
	public final void decreaseTimeTwo() {
		if (time1 > 0) {
			time1--;
		}
		repaint();
	}

	/**
	 * zjistuje jestli je na tahu prvni hrac
	 * 
	 * @return jestli je na tahu
	 */
	public final boolean isFirstActive() {
		return firstActive;
	}

	/**
	 * Nastavuje ze hraje prvni hrac
	 * 
	 * @param firstActive
	 *            hraje
	 */
	public final void setFirstActive(final boolean firstActive) {
		this.firstActive = firstActive;
		this.secondActive = false;
	}

	/**
	 * zjistuje jestli je na tahu druhy hrac
	 * 
	 * @return jestli je na tahu
	 */
	public final boolean isSecondActive() {
		return secondActive;
	}

	/**
	 * Nastavuje ze hraje druhy hrac
	 * 
	 * @param firstActive
	 *            hraje
	 */
	public final void setSecondActive(final boolean secondActive) {
		this.secondActive = secondActive;
		this.firstActive = false;
	}

	/**
	 * Meni pozici informaci o hracich
	 * 
	 * @param flip
	 */
	public final void setFlip(final boolean flip) {
		this.flip = flip;
		repaint();
	}

	/**
	 * Nastavuje vysku panelu
	 * 
	 * @param height
	 *            vyska
	 */
	public final void setHeight(final int height) {
		this.height = height;
		repaint();
	}

	/**
	 * Nastavuje pozici informacniho panelu
	 * 
	 * @param position
	 *            pozice panelu
	 */
	public final void setPosition(final int position) {
		this.position = position + 40;
		repaint();
	}

	/**
	 * Rating hrace 1
	 * 
	 * @return
	 */
	public final String getRating1() {
		return rating1;
	}

	/**
	 * Rating hrace 1
	 * 
	 * @param rating1
	 */
	public final void setRating1(final String rating1) {
		this.rating1 = rating1;
		Logger.getRootLogger().debug("rating1:" + rating1);
	}

	/**
	 * Rating hrace 2
	 * 
	 * @return jeho rating
	 */
	public final String getRating2() {
		return rating2;
	}

	/**
	 * Nastaveni ratingu hrace 2
	 * 
	 * @param rating2
	 */
	public final void setRating2(final String rating2) {
		this.rating2 = rating2;
		Logger.getRootLogger().debug("rating2:" + rating2);
	}

	/**
	 * Sila figur hrace 1
	 * 
	 * @param materialStrength1
	 *            sila figur
	 */
	public void setMaterialStrength1(String materialStrength1) {
		this.materialStrength1 = materialStrength1;
	}

	/**
	 * Sila figur hrace 2
	 * 
	 * @param materialStrength2
	 *            sila figur
	 */
	public void setMaterialStrength2(String materialStrength2) {
		this.materialStrength2 = materialStrength2;
	}
}
